package com.freesky.common.redis;

import com.alibaba.fastjson.JSON;
import com.freesky.common.vo.CartItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Component
public class RedisTemplate implements IRedisTemplate {
    private Logger logger = LoggerFactory.getLogger(RedisTemplate.class);
    private JedisPool jedisPool;

    public RedisTemplate(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    /**
     * 向缓存中增加对象
     *
     * @param key
     * @param t
     * @param <T>
     */
    @Override
    public <T> void putCache(String key, T t) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), JSON.toJSONBytes(t));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

//    /**
//     * 向缓存中增加集合
//     * @param key
//     * @param list
//     * @param <T>
//     */
//    @Override
//    public <T > void putCacheList(String key, List<T> list) {
//        Jedis jedis = null;
//        try {
//            jedis = jedisPool.getResource();
//            jedis.set(key.getBytes(), JSON.toJSONBytes(list));
//        } catch (Exception e) {
//            logger.error(e.getMessage(), e);
//        } finally {
//            if (jedis != null) {
//                jedis.close();
//            }
//        }
//    }


    /**
     * 从缓存中获取缓存对象
     *
     * @param key
     * @param clazz
     * @return
     */
    @Override
    public <T> T getCacheObject(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            if (null == bytes) {
                return null;
            }
            return JSON.parseObject(bytes, clazz);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    /**
     * 从缓存中获取缓存对象集合
     *
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    @Override
    public <T> List<T> getCacheList(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            if (null == bytes) {
                return null;
            }
            return JSON.parseArray(new String(bytes, "UTF-8"), clazz);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    /**
     * 从缓存中集合，缓存对象
     *
     * @param key
     */
    @Override
    public Long delete(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    @Override
    public Long hdel(String key, String... field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hdel(key.getBytes(), JSON.toJSONBytes(field));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    @Override
    public Long expire(String key, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.expire(key.getBytes(), seconds);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    @Override
    public Boolean hexists(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hexists(key.getBytes(), field.getBytes());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return false;
    }

    @Override
    public <T> T hget(String key, String field, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.hget(key.getBytes(), field.getBytes());
            if (null == bytes) {
                return null;
            }
            return JSON.parseObject(bytes, clazz);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    @Override
    public <T> Long hset(String key, String field, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hset(key.getBytes(), field.getBytes(), JSON.toJSONBytes(value));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }

    @Override
    public <T> List<T> hvals(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            List<String> jsonList = jedis.hvals(key);
            List<T> list = new ArrayList<>();
            if (jsonList != null) {
                for (String json : jsonList) {
                    T obj = JSON.parseObject(json, clazz);
                    list.add(obj);
                }
            }
            return list;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }


}
